import {defineStore} from "pinia";
import {ref} from "vue";
import type {UserInfo} from "../types/account";
import {Action, Permissions} from "../types/permissions.ts";

export const useUserStore = defineStore("user", () => {
    const userInfo = ref<UserInfo>()

    function setUserInfo(val: UserInfo) {
        userInfo.value = val
    }

    function getUserInfo() {
        return userInfo.value;
    }

    function checkPermission(module: Permissions | undefined, action: Action) {
        if (module === undefined) {
            return false;
        }
        return (userInfo.value?.permissions.indexOf(module + action) ?? -1) !== -1;
    }


    return {
        setUserInfo,
        getUserInfo,
        checkPermission,
        userInfo
    }
}, {
    persist: true
})